package com.edocbase.desktop.lightweight.widgets.richeditor.commands;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;

public abstract class DocumentCommand implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3210138017149312619L;
	protected CommandInvoker invoker;
	protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);
	private CommandState state;
	protected Object returnObject = null;
	public abstract String getCommandIdentifier();

	public boolean showUI() {
		return false;
	}

	public Object getValue() {
		return null;
	}
	
	public Object getReturnValue(){
		return returnObject;
	}

	public CommandInvoker getInvoker() {
		return invoker;
	}

	public void setInvoker(CommandInvoker editor) {
		this.invoker = editor;
		if (trackCommand()) {
			editor.trackCommand(this);
		}
	}

	protected boolean trackCommand() {
		return true;
	}

	public CommandState getState() {
		return state;
	}

	public void setState(CommandState state) {
		CommandState oldValue = this.state;
		this.state = state;
		firePropertyChange("state", oldValue, state);
	}

	public void dispose() {
		// does nothing by default
	}

	public void addPropertyChangeListener(final PropertyChangeListener l) {
		if (l == null) {
			throw new IllegalArgumentException();
		}
		listeners.addPropertyChangeListener(l);
	}

	public void removePropertyChangeListener(final PropertyChangeListener l) {
		listeners.removePropertyChangeListener(l);
	}

	protected void firePropertyChange(final String prop, final Object old,
			final Object newValue) {
		if (listeners.hasListeners(prop)) {
			listeners.firePropertyChange(prop, old, newValue);
		}
	}

	public void execute() {
		invoker.execute(this);
	}
}
